<?php
/*
 * Created on Mar 27, 2009
 * Author: ANH
 * fileName: Gallery.php
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
class gallery_Model_Gallery extends Clu_Db_Table_Abstract
{
	public function init()
	{
		require (APP_PATH."/library/common.php");
	}
	public function postImage($createWM)
	{
		$watermarkDir = APP_PATH."/public/watermark/";
		$auth = Zend_Auth::getInstance();
		$userId = $auth->getIdentity()->id;
		$configSize = array("xsmall", "small", "medium", "large", "xlarge");
		$configSizeInPixel = array("75", "100", "260", "350", "600", "1300");
		$ustoreDir = APP_PATH."/public/ustore/$userId/images/original/gallery/";
		if (!file_exists($ustoreDir))
			mkdir($ustoreDir, 0777  , true);
			
		$adapter= new Zend_File_Transfer_Adapter_Http();
		$IsImageValidateor = new Clu_Validate_File_IsImage ();
		$isImage = $IsImageValidateor->isValid($_FILES['file_upload']['tmp_name'], $_FILES['file_upload']);
		$extension_path = pathinfo($_FILES['file_upload']['name']);
		$name = $extension_path['filename'];
		if($isImage)
		{
			$mimeType = $IsImageValidateor->getFileMimeType();
			$extension = explode('/',$mimeType);

			$fileName = $name.'.'.$extension['1'];
			$newfileName = SanitizeFileName($fileName);
			$originalFilename = $ustoreDir.$newfileName;
			$i = 1;
			while(file_exists($originalFilename))
			{
				$fileName = $name.'_'.$i.'.'.$extension['1'];
				$newfileName = SanitizeFileName($fileName);
				$originalFilename = $ustoreDir.$newfileName;
				$i++;
			}
			
			try 
			{
				$adapter->addFilter('Rename', array('target' => $originalFilename, 'overwrite' => true));
				$adapter->receive();
			} 
			catch (Zend_File_Transfer_Exception $e) 
			{
			     $e->getMessage();
			}
			chmod($originalFilename, 0666);
		}
		
		generate_image_copies($ustoreDir, $newfileName, $watermarkDir, $configSize, $configSizeInPixel, $userId, $createWM);
	}
}

?>
